package com.taobao.askwho.utils.data;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class WorkflowXmlUtil {
	static Logger logger = Logger.getLogger(WorkflowXmlUtil.class);
	public static Map<String, String> loadWorkFlowXml() {
		/**
		 * Xml范例
		 <apply id="99c47d4f-4e86-4e98-8a72-3c0c516c808c" itemid="957879d3-8eb8-4ddf-a5d1-7118f519179d">
			<applyname>detail</applyname>
			<serviceType>产品线</serviceType>
			<oneProduct>淘宝网-导购</oneProduct>
			<twoProduct>商品详情</twoProduct>
			<TL>小凡</TL>
			<testTL>鸾伽</testTL>
			<SCM>红巾</SCM>
			<SQA>临云</SQA>
			<UED>渐飞</UED>
			<applyPic>小凡</applyPic>
			<PE>普智</PE>
			<appOps>小凡</appOps>
			<appdesc>宝贝详情。</appdesc>
		</apply>
		 */
		
		final Map<String, String> nodeNameMap = new HashMap<String, String>();
		nodeNameMap.put("tl", "日常PM,TL");
		nodeNameMap.put("testtl", "测试TL");
		nodeNameMap.put("scm", "应用SCM");
		nodeNameMap.put("sqa", "过程改进,SQA");
		nodeNameMap.put("appops", "应用负责人,appops");
		nodeNameMap.put("pe", "PE");
		final String apply = "apply";
		
		final Map<String, String> tagMap = new HashMap<String, String>();
		final String applyname = "applyname";
		final String twoProduct = "twoproduct";
		
		try {
			String[] fileArray = new String[] {
					"F:/data/askwhodata/产品线下的应用.xml",
					"F:/data/askwhodata/公共服务下的应用.xml" };
			for (String fileString : fileArray) {
				File file = new File(fileString);
				DocumentBuilderFactory dbf = DocumentBuilderFactory
						.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();
				Document doc = db.parse(file.toURI().toString());
				NodeList list1 = doc.getElementsByTagName(apply);
				System.out.println("list1.getLength()=" + list1.getLength());
				for (int i = 0; i < list1.getLength(); i++) {
					Node node = list1.item(i);
					NodeList childList = node.getChildNodes();
					Map<String, String> nodeNameMapTmp = new HashMap<String, String>();
					for (int j = 0; j < childList.getLength(); j++) {
						Node childNode = childList.item(j);
						String nodeName = childNode.getNodeName().trim()
								.toLowerCase();
						String value = childNode.getTextContent();
						if (StringUtils.isBlank(value)) {
							logger.info("nodeName=" + nodeName
									+ ", value is empty");
							continue;
						} else {
							nodeNameMapTmp.put(nodeName, value);
						}
					}
					String appNameTmp = nodeNameMapTmp.get(applyname);
					//System.out.println("**" + applyname + "\t" + appNameTmp);
					String twoProductTmp = nodeNameMapTmp.get(twoProduct);
					if (appNameTmp == null) {
						//System.out.println("node applyname == null");
						continue;
					} else {
						for (String key : nodeNameMap.keySet()) {
							if (nodeNameMapTmp.containsKey(key)) {
								tagMap.put(
										appNameTmp + "," + nodeNameMap.get(key),
										nodeNameMapTmp.get(key));
								if (twoProductTmp != null) {
									tagMap.put(twoProductTmp + ","
											+ nodeNameMap.get(key),
											nodeNameMapTmp.get(key));
								}
							} else {
								//System.out.println(appNameTmp + "的" + key + "值为空");							
							}
						}
					}
				}
			}
		} catch (Exception e) {
			logger.error("出现的异常", e);
		}
		//		int i=1;
//		for(Entry<String, String> entry : tagMap.entrySet()) {
//			System.out.println(i++ + "\t" +entry.getKey() + "\t" + entry.getValue());
//		}
		System.out.println("tagMap.size() == " + tagMap.size());
		return tagMap;
	}
	
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		WorkflowXmlUtil.loadWorkFlowXml();
	}
}
